﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;

namespace ListOfTale
{
    /// <summary>
    /// Interaction logic for ChartViewer.xaml
    /// </summary>
    public partial class ChartViewer : Window
    {
        public ChartViewer()
        {
            InitializeComponent();
            for (int i = 1918; i <= 2012; i++)
            {
                StartYearCmb.Items.Add(i);
                EndYearCombobox.Items.Add(i);
            }
            DataTypeCmb.Items.Add("Тираж");
            DataTypeCmb.Items.Add("Издания");
            DataTypeCmb.SelectedIndex = 0;
            StartYearCmb.SelectedIndex = 0;
            EndYearCombobox.SelectedIndex = EndYearCombobox.Items.Count-1;
        }

        public SerData[] BuildChartData(int startYear,int endYear)
        {
           
            SerData[] sers = null;
            if (DataTypeCmb.Text == "Тираж")
            {
                TalesDataSetTableAdapters.DataTable2TableAdapter dt = new TalesDataSetTableAdapters.DataTable2TableAdapter();
                var print = dt.GetPrintStat(startYear, endYear);
                SerData s1 = new SerData();
                s1.points = print.Where(row => row.Name == "Авторская сказка").Select(r => new ChartPoint() { X = r.Year, Y = Convert.ToInt32(r.Printing) }).ToArray();
                s1.Name = "Авторская сказка";
                SerData s2 = new SerData();
                s2.points = print.Where(row => row.Name != "Авторская сказка").GroupBy(r => r.Year).Select(rr => new ChartPoint() { X = rr.Key, Y = rr.Sum(s => Convert.ToInt32(s.Printing)) }).ToArray();
                s2.Name = "Народная сказка";
                sers = new SerData[2];
                sers[0] = s1;
                sers[1] = s2;

            }
            else
                if (DataTypeCmb.Text == "Издания")
                {
                    TalesDataSetTableAdapters.DataTable1TableAdapter dt = new TalesDataSetTableAdapters.DataTable1TableAdapter();
                    var print = dt.GetBookStat(startYear, endYear);
                    SerData s1 = new SerData();
                    s1.points = print.Where(row => row.Name == "Авторская сказка").Select(r => new ChartPoint() { X = r.Year, Y = Convert.ToInt32(r.Books) }).ToArray();
                    s1.Name = "Авторская сказка";
                    SerData s2 = new SerData();
                    s2.points = print.Where(row => row.Name != "Авторская сказка").GroupBy(ss=>ss.Year).Select(r => new ChartPoint() { X = r.Key, Y = Convert.ToInt32(r.Sum(ss=>Convert.ToInt32( ss.Books))) }).ToArray();
                    s2.Name = "Народная сказка";
                    sers = new SerData[2];
                    sers[0] = s1;
                    sers[1] = s2;
                }
                else
                {
                    MessageBox.Show("нужно выбрать параметр в выпадающем списке");
                }
            return sers;
        }



        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            int startyear =Convert.ToInt32( StartYearCmb.Text);
            int endyear = Convert.ToInt32(EndYearCombobox.Text);
            var sers = BuildChartData(startyear,endyear);
            for (int i = 0; i < sers.Length; i++)
            {
                (StatChart.Series[i] as LineSeries).ItemsSource = sers[i].points;
                (StatChart.Series[i] as LineSeries).Refresh();
                (StatChart.Series[i] as LineSeries).Title = sers[i].Name;
            }
        }
    }
    public class SerData
    {
        public string Name { get; set; }
        public ChartPoint[] points { get; set; }
    }
    public class ChartPoint
    {
        public int X { get; set; }
        public long Y { get; set; }
    }
}
